package dao

import (
	"my-desktop/config"
	"my-desktop/models"
)

// 用户注册
func Register(user models.SysUser) (data *models.SysUser, err error) {
	_, err = config.DB.Exec("insert into sys_user values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", user.UserID, user.UserName, user.NickName,
		user.Email, user.PhoneNumber, user.Sex, user.Avatar, user.Password, user.DelFlag, user.Status, user.LoginIP, user.LoginDate, user.CreateBy,
		user.CreateTime, user.UpdateBy, user.UpdateTime, user.Remark)
	return nil, err
}

// 用户登录
func Login(username, password string) (data *models.SysUser, err error) {
	row := config.DB.QueryRow("select  * from sys_user where username=? and password = ?", username, password)
	user := &models.SysUser{}
	err = row.Scan(
		&user.UserID, &user.UserName, &user.NickName, &user.Email, &user.PhoneNumber, &user.Sex, &user.Avatar, &user.Password,
		&user.DelFlag, &user.Status, &user.LoginIP, &user.LoginDate, &user.CreateBy, &user.CreateTime, &user.UpdateBy, &user.UpdateTime, &user.Remark,
	)
	return user, err
}

// 根据用户名查找密码
func GetUserByUserName(username string) (data *models.SysUser, err error) {
	row := config.DB.QueryRow("select  * from sys_user where username = ?", username)
	user := &models.SysUser{}
	err = row.Scan(
		&user.UserID, &user.UserName, &user.NickName, &user.Email, &user.PhoneNumber, &user.Sex, &user.Avatar, &user.Password,
		&user.DelFlag, &user.Status, &user.LoginIP, &user.LoginDate, &user.CreateBy, &user.CreateTime, &user.UpdateBy, &user.UpdateTime, &user.Remark,
	)
	return user, err
}

// 根据用户ID查找密码
func GetUserByUserID(userID uint64) (data *models.SysUser, err error) {
	row := config.DB.QueryRow("select  * from sys_user where user_id = ?", userID)
	user := &models.SysUser{}
	err = row.Scan(
		&user.UserID, &user.UserName, &user.NickName, &user.Email, &user.PhoneNumber, &user.Sex, &user.Avatar, &user.Password,
		&user.DelFlag, &user.Status, &user.LoginIP, &user.LoginDate, &user.CreateBy, &user.CreateTime, &user.UpdateBy, &user.UpdateTime, &user.Remark,
	)
	return user, err
}
